import { useLocalStorage } from '@vueuse/core'
import { defineStore } from 'pinia'
import { changeLocale as changeLocaleI18n } from '@/locales/util'

export const useLocaleStore = defineStore('locale', () => {
  // 缓存中的语言
  const locale = useLocalStorage<string>('LOCALE', navigator.language)

  // 切换语言
  const changeLocale = async (newLocale: string) => {
    locale.value = newLocale
    await changeLocaleI18n(locale.value)
  }

  return {
    locale,
    changeLocale,
  }
})
